/*
  Copyright 2011 fullkawa

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package jp.fullkawa.xnovel;

import java.util.ArrayList;
import java.util.List;

import jp.fullkawa.xnovel.content.Content;
import jp.fullkawa.xnovel.content.Sentence;
import jp.fullkawa.xnovel.exception.IllegalLayerIdException;
import jp.fullkawa.xnovel.picture.Image;
import jp.fullkawa.xnovel.picture.Picture;

/**
 * ノベルを構成する最小単位
 * @author fullkawa
 */
public class Cut implements Cloneable
{
	public final static String LAYER_BG_ID = "bg";
	public final static String LAYER_0_ID = "0";
	public final static String LAYER_1_ID = "1";
	public final static String LAYER_2_ID = "2";

	private final int LAYER_BG = 0;
	private final int LAYER_0 = 1;
	private final int LAYER_1 = 2;
	private final int LAYER_2 = 3;

	private Integer number;
	private String id;

	public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
        this.setId("cut:" + Integer.toString(number));
    }

    public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	/**
	 * レイヤ(映像)
	 */
	private Layer[] layers = {
		new Layer(LAYER_BG_ID),
		new Layer(LAYER_0_ID),
		new Layer(LAYER_1_ID),
		new Layer(LAYER_2_ID)
	};

	public void setPicture(String layer_id, Picture picture)
	{
		Layer layer = this.getLayer(layer_id);
		layer.setPicture(picture);
		layer.setChange(true);
	}

	public void clearPicture(String layer_id)
	{
		Layer layer = this.getLayer(layer_id);
		layer.setPicture(new Image());
	}

	private Layer getLayerBg() {
		return layers[LAYER_BG];
	}
	private Layer getLayer0() {
		return layers[LAYER_0];
	}
	private Layer getLayer1() {
		return layers[LAYER_1];
	}
	private Layer getLayer2() {
		return layers[LAYER_2];
	}
	public Layer[] getLayers() {
		return layers;
	}
	public void setLayers(Layer[] layers) {
		this.layers = layers;
	}

	public Layer getLayer(String layer_id)
	{
		if (LAYER_BG_ID.equals(layer_id)) {
			return this.getLayerBg();
		}
		else if (LAYER_0_ID.equals(layer_id)) {
			return this.getLayer0();
		}
		else if (LAYER_1_ID.equals(layer_id)) {
			return this.getLayer1();
		}
		else if (LAYER_2_ID.equals(layer_id)) {
			return this.getLayer2();
		}
		else {
			throw new IllegalLayerIdException(layer_id);
		}
	}

	/**
	 * 音声
	 */
	private Audio audio;

	public Audio getAudio() {
		return audio;
	}

	public void setAudio(Audio audio)
	{
		this.audio = audio;
		this.audio.setChange(true);
	}

	public void clearAudio()
	{
		this.setAudio(new Audio());
	}

	/**
	 * 文章など
	 */
	private List<Content> contents = new ArrayList<Content>();

	public void addContent(Content content)
	{
		contents.add(content);
	}

	public void addContent(String sentence)
	{
		Sentence content = new Sentence(sentence);
		this.addContent(content);
	}

	public List<Content> getContents() {
		return contents;
	}

	public void setContents(List<Content> contents) {
		this.contents = contents;
	}

	@Override
	public Cut clone()
	{
		Cut clone = null;
		try {
			clone = (Cut)super.clone();

			clone.audio = this.audio.clone();

			List<Layer> layers = new ArrayList<Layer>();
			for (Layer src_layer : this.layers)
			{
				layers.add(src_layer.clone());
			}
			clone.setLayers(layers.toArray(new Layer[0]));

			// contents は clone しない
			clone.setContents(new ArrayList<Content>());

		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return clone;
	}

}
